package com.star.spring.deferred.service.thread.safe;


import com.star.spring.deferred.utils.CommonUtils;

/**
 * @description: 不安全的例子
 * @author: star
 * @create: 2025-01-01 23:18
 **/

public class NoSafeDemo {

    private static int result = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                result++;

            }
        });
        thread1.setName("thread1");

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                result--;

            }
        });
        thread2.setName("thread2");

        thread1.start();
        thread2.start();
        // 等待两个线程都执行完成
        thread1.join();
        thread2.join();

        CommonUtils.log("最终结果为：" + result);

    }
}
